#
# Cookbook Name:: logstash
# Recipe:: install
#
# Copyright 2013, IBM
#
# All rights reserved - Do Not Redistribute
#

cuser = node[:chef][:user]
cgroup = node[:chef][:group]
deployment = node[:deployment]

startinstr "Install logstash"

directory "/tmp/logstash" do
  user cuser
  group cgroup
  action :create
end

bash "Fetch logstash" do
  user cuser
  group cgroup
  cwd "/tmp/"
  creates "/tmp/logstash_download.complete"
  code <<-EOH
		scp -r #{node[:common][:scp_repo]}/downloads/logstash/* /tmp/logstash
		sudo mkdir -p /tmp/logstash
		sudo cp /tmp/logstash/#{node[:logstash][:binary]} /tmp/logstash
		if [ $? == 0 ]; then
			touch /tmp/logstash_download.complete
		fi
  EOH
end

bash "Extract logstash package" do
  user cuser
  group cgroup
  cwd "/tmp/logstash"
  code <<-EOH
      sudo tar -xzf #{node[:logstash][:binary]} -C /opt
  EOH
end

bash "Backup original grok-patterns" do
  user cuser
  group cgroup
  cwd "#{node['logstash']['dir']}/patterns"
  code <<-EOH
      sudo cp grok-patterns grok-patterns.bak
  EOH
end

cookbook_file "/opt/#{node[:logstash][:version]}/patterns/webspherePatterns" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "webspherePatterns"
  action :create
end

cookbook_file "/opt/#{node[:logstash][:version]}/patterns/grok-patterns" do
  backup false
  owner cuser
  group cgroup
  mode 0755
  source "grok-patterns"
  action :create
end

stopinstr "Install logstash"
